package zhihui.mobile.presenter.shop;

import android.content.Context;
import android.content.Intent;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.page.PageInfo;
import zhihui.mobile.bean.shop.CategoryShopInfo;
import zhihui.mobile.bean.shop.SkuInfo;
import zhihui.mobile.bean.shop.SpuInfo;
import zhihui.mobile.bean.shop.address.ShoppingAddress;
import zhihui.mobile.bean.shop.cart.CartInfo;
import zhihui.mobile.bean.shop.detail.ShopDetail;
import zhihui.mobile.bean.shop.order.ShopOrderDetail;
import zhihui.mobile.bean.shop.sales.CouponInfo;
import zhihui.mobile.bean.shop.sales.SalesListData;
import zhihui.mobile.interfaces.net.IHttpCallBackListener;
import zhihui.mobile.manager.shop.CouponManager;
import zhihui.mobile.manager.shop.ShopAddressManager;
import zhihui.mobile.manager.shop.ShopManager;
import zhihui.mobile.manager.shop.ShopOrderManager;
import zhihui.mobile.manager.shop.ShopSalesManager;
import zhihui.mobile.ui.IView;
import zhihui.mobile.ui.shop.cart.ShoppingCartActivity;
import zhihui.mobile.ui.shop.coupon.CouponActivity;
import zhihui.mobile.ui.shop.details.ShopDetailsActivity;
import zhihui.mobile.ui.shop.list.ShopListActivity;
import zhihui.mobile.ui.shop.order.ShopOrderDetailsActivity;
import zhihui.mobile.ui.shop.sales.ShopSalesActivity;

/**
 * Created by 张公铭 on 2017/8/24 0024.
 * 通用事件presenter
 */

public class BaseShopPresenter
{
    /**
     * 商品详情
     */
    public void goShopDetails(final Context context, int shopId, int salesId, final IView view)
    {
        view.showLoading();
        ShopManager.getInstance().getShopDetailsByShopId(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                view.hindLoading();
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    Gson gson = new Gson();
                    ShopDetail shopDetail = gson.fromJson(data.toString(), ShopDetail.class);
                    ShopManager.getInstance().setCurShop(shopDetail);
                    Intent intent = new Intent(context, ShopDetailsActivity.class);
                    context.startActivity(intent);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    view.showMessage(context.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                view.hindLoading();
                view.showMessage(message);
            }
        }, shopId, salesId);

    }

    /**
     * 订单详情页
     */
    public void goShopOrderDetail(final Context context, String orderId, final IView view)
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(context))
        {
            return;
        }
        view.showLoading();
        ShopOrderManager.getInstance().getOrderDetail(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                view.hindLoading();
                ShopOrderDetail detail;
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    Gson gson = new Gson();
                    detail = gson.fromJson(data.getJSONObject("orderInfo").toString(), ShopOrderDetail.class);
                    List<SkuInfo> list = gson.fromJson(data.getJSONArray("goodsList").toString(), new TypeToken<List<SkuInfo>>()
                    {
                    }.getType());
                    detail.setGoodsList(list);
                    ShopOrderManager.getInstance().setCurOrderDetail(detail);
                    Intent intent = new Intent(context, ShopOrderDetailsActivity.class);
                    context.startActivity(intent);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    view.showMessage(context.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                view.hindLoading();
                view.showMessage(message);
            }
        }, orderId);
    }

    /**
     * 活动界面
     */
    public void goSalesActivity(final Context mContext, final IView view, final int type)
    {
        view.showLoading();
        ShopSalesManager.getInstance().getSalesListByType(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                view.hindLoading();
                Gson gson = new Gson();
                try
                {
                    List<SalesListData> list = gson.fromJson(result.getJSONArray("data").toString(), new TypeToken<List<SalesListData>>()
                    {
                    }.getType());
                    ShopSalesManager.getInstance().setmList(list);
                    Intent intent = new Intent(mContext, ShopSalesActivity.class);
                    intent.putExtra("salesType", type);
                    mContext.startActivity(intent);
                } catch (JSONException e)
                {
                    view.showMessage(mContext.getString(R.string.json_error));
                    e.printStackTrace();
                }
            }

            @Override
            public void failed(String message)
            {
                view.hindLoading();
                view.showMessage(message);
            }
        }, type);
    }

    /**
     * 购物车
     */
    public void goShopCart(final IView view, final Context context)
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(context))
        {
            return;
        }
        view.showLoading();
        ShopManager.getInstance().getCartShopList(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                view.hindLoading();
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    CartInfo cartInfo = new CartInfo();
                    cartInfo.setCartNum(data.getInt("cartNum"));
                    Gson gson = new Gson();
                    List<SkuInfo> list = gson.fromJson(data.getJSONArray("skus").toString(), new TypeToken<List<SkuInfo>>()
                    {
                    }.getType());
                    cartInfo.setSkus(list);
                    ShopManager.getInstance().setCartInfo(cartInfo);
                    Intent intent = new Intent(context, ShoppingCartActivity.class);
                    context.startActivity(intent);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    view.showMessage(context.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                view.hindLoading();
                view.showMessage(message);
            }
        });
    }

    /**
     * 我的优惠券
     */
    public void goMySales(final Context mContext, final IView view)
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(mContext))
        {
            return;
        }
        view.showLoading();
        CouponManager.getInstance().getUseCouponList(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                view.hindLoading();
                Gson gson = new Gson();
                try
                {
                    List<CouponInfo> list = gson.fromJson(result.getJSONArray("data").toString(), new TypeToken<List<CouponInfo>>()
                    {
                    }.getType());
                    CouponManager.getInstance().setmList(list);
                    Intent intent = new Intent(mContext, CouponActivity.class);
                    mContext.startActivity(intent);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    view.showMessage(mContext.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                view.hindLoading();
                view.showMessage(message);
            }
        }, "", 0, 5101);
    }

    public interface IAddressSuccessListener
    {
        void success(List<ShoppingAddress> list);
    }

    /**
     * 获取收货地址
     */
    public void getAddress(final IView view, final Context context, final IAddressSuccessListener listener)
    {
        if (!zhihui.mobile.ui.utils.UiUtils.cheeckLogin(context))
        {
            return;
        }
        view.showLoading();
        ShopAddressManager.getInstance().getUserAddress(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                view.hindLoading();
                Gson gson = new Gson();
                try
                {
                    List<ShoppingAddress> list = gson.fromJson(result.getJSONArray("data").toString(), new TypeToken<List<ShoppingAddress>>()
                    {
                    }.getType());
                    ShopAddressManager.getInstance().setmList(list);
                    listener.success(list);
                } catch (JSONException e)
                {
                    view.showMessage(context.getString(R.string.json_error));
                }
            }

            @Override
            public void failed(String message)
            {
                view.hindLoading();
                view.showMessage(message);
            }
        });
    }

    public void goShopCategoryActivity(final IView view, final Context context, final String categoryId)
    {
        view.showLoading();
        ShopManager.getInstance().getShopListByCategoryId(new IHttpCallBackListener()
        {
            @Override
            public void success(JSONObject result)
            {
                Gson gson = new Gson();
                try
                {
                    JSONObject data = result.getJSONObject("data");
                    PageInfo pageInfo = gson.fromJson(data.toString(), PageInfo.class);
                    List<SpuInfo> list = gson.fromJson(data.getJSONArray("list").toString(), new TypeToken<List<SpuInfo>>()
                    {
                    }.getType());
                    CategoryShopInfo categoryShopInfo = new CategoryShopInfo();
                    categoryShopInfo.setParam(categoryId);
                    categoryShopInfo.setPageInfo(pageInfo);
                    categoryShopInfo.setList(list);
                    ShopManager.getInstance().setCategoryShopInfo(categoryShopInfo);
                    Intent intent = new Intent(context, ShopListActivity.class);
                    context.startActivity(intent);
                } catch (JSONException e)
                {
                    e.printStackTrace();
                    view.showMessage(context.getString(R.string.json_error));
                }
                view.hindLoading();
            }

            @Override
            public void failed(String message)
            {
                view.hindLoading();
                view.showMessage(context.getString(R.string.http_error));
            }
        }, categoryId, 1, "", "desc");
    }
}
